<div class="config-container config-materials">
<h3 class="section-title material-title">Materials</h3>

<table class="list_table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Type</th>
        <th>URL</th>
      <% unless @is_config_repo_pipeline %>
        <th class="remove">Remove</th>
      <% end %>
    </tr>
    </thead>
    <tbody>
    <% for material in @pipeline.materialConfigs() %>
        <tr>
            <td>
                <% if material.getType() == 'PluggableSCMMaterial' %>
                    <a class="material_name wrapped_word" href="#" onclick="Util.ajax_modal('<%= admin_pluggable_scm_edit_path(:finger_print => material.getPipelineUniqueFingerprint()) -%>', {readOnly: <%= @is_config_repo_pipeline || false %>, overlayClose: false, title: '<%='Edit Material' + " - " + com.thoughtworks.go.plugin.access.scm.SCMMetadataStore.getInstance().displayValue(material.getPluginId())-%>'}, function(text){return text})">
                        <%= material.getDisplayName() -%>
                    </a>
                <% else %>
                <a class="material_name wrapped_word" href="#" onclick="Util.ajax_modal('<%= admin_material_edit_path(material) -%>', {readOnly: <%= @is_config_repo_pipeline || false %>, overlayClose: false, title: '<%='Edit Material' + " - " + material.getTypeForDisplay() -%>'}, function(text){return text})">
                    <%= material.getDisplayName() -%>
                </a>
                <% end %>
            </td>
            <td><%= material.getTypeForDisplay() %></td>
            <td class="wrapped_word"><%= material.getUriForDisplay() %></td>
          <% unless @is_config_repo_pipeline %>
            <td>
                <% if @pipeline.materialConfigs().size() <= 1 %>
                    <%= action_icon({:type => "delete", :disabled => true, :title => 'Cannot delete this material as pipeline should have at least one material'}) %>
                <% else %>
                    <% if material.isUsedInFetchArtifact(@pipeline)%>
                        <%= action_icon({:type => "delete", :disabled => true, :title => 'Cannot delete this material since it is used in a fetch artifact task.'}) %>
                    <% else %>
                        <% if material.isUsedInLabelTemplate(@pipeline) %>
                            <%= action_icon({:type => "delete", :disabled => true, :title => "Cannot delete this material as the material name is used in this pipeline's label template"}) %>
                        <% else %>
                            <%= form_for material, :as => '', :url => admin_material_delete_path(:pipeline_name => @pipeline.name(), :finger_print => material.getPipelineUniqueFingerprint()),
                                        :html => {:id => "delete_material_#{material.getPipelineUniqueFingerprint()}", :method => "delete"} do |f| %>
                                <%= md5_field %>
                                <span class="icon_remove delete_parent" title="Remove this material" id="trigger_delete_material_<%= material.getPipelineUniqueFingerprint() %>">
                                    <%= render :partial => 'admin/shared/submit_with_prompt.html',
                                               :locals => {:scope => {
                                                       :button_id => "trigger_delete_material_#{material.getPipelineUniqueFingerprint()}",
                                                       :message => "Are you sure you want to delete the '#{material.getTypeForDisplay()}' material at '#{material.getUriForDisplay()}' ? ",
                                                       :on_confirmation_js => "$('#{"delete_material_#{material.getPipelineUniqueFingerprint()}"}').submit()"}} %>
                                </span>
                            <% end %>
                        <% end %>
                    <% end %>
                <% end %>
            </td>
          <% end %>
        </tr>
    <% end %>
    <tr>
        <td colspan="4">
    <% unless @is_config_repo_pipeline %>
			<%= action_icon({:type => "add", :id => "add_material_dropdown_link", :text => 'Add Material',:menu_link => true,  :href => "#"}) %>
    <% end %>
            <div id="new_material_popup" class="hidden enhanced_dropdown">
                <ul>
                    <% if @pipeline.materialConfigs().scmMaterialsHaveDestination() %>
                        <li><%= link_to 'Git', '#', :onclick => "Modalbox.show('#{admin_git_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Git'}'})" -%></li>
                        <li><%= link_to 'Subversion', '#', :onclick => "Modalbox.show('#{admin_svn_new_path(:controller => "admin/materials/svn", :action => "new")}', {overlayClose: false, title: '#{'Add Material'} - #{'Subversion'}'})" -%></li>
                        <li><%= link_to 'Mercurial', '#', :onclick => "Modalbox.show('#{admin_hg_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Mercurial'}'})" -%></li>
                        <li><%= link_to 'Perforce', '#', :onclick => "Modalbox.show('#{admin_p4_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Perforce'}'})" -%></li>
                        <li><%= link_to 'Team Foundation Server', '#', :onclick => "Modalbox.show('#{admin_tfs_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Team Foundation Server'}'})" -%></li>
                        <% scm_plugin_ids = com.thoughtworks.go.plugin.access.scm.SCMMetadataStore.getInstance().getPlugins() %>
                        <% if (!scm_plugin_ids.isEmpty()) %>
                            <% for scm_plugin_id in scm_plugin_ids %>
                                <% scm_name = com.thoughtworks.go.plugin.access.scm.SCMMetadataStore.getInstance().displayValue(scm_plugin_id) %>
                                <li><%= link_to scm_name, '#', :onclick => "Modalbox.show('#{admin_pluggable_scm_new_path(:plugin_id => scm_plugin_id)}', {overlayClose: false, title: '#{'Add Material'} - #{scm_name}'})" -%></li>
                            <% end %>
                        <% end %>
                    <% else %>
                        <li><%= link_to 'Git', '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{'Git'}'})" -%></li>
                        <li><%= link_to 'Subversion', '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{'Subversion'}'})" -%></li>
                        <li><%= link_to 'Mercurial', '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{'Mercurial'}'})" -%></li>
                        <li><%= link_to 'Perforce', '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{'Perforce'}'})" -%></li>
                        <li><%= link_to 'Team Foundation Server', '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{'Team Foundation Server'}'})" -%></li>
                        <% scm_plugin_ids = com.thoughtworks.go.plugin.access.scm.SCMMetadataStore.getInstance().getPlugins() %>
                        <% if (!scm_plugin_ids.isEmpty()) %>
                            <% for scm_plugin_id in scm_plugin_ids %>
                                <% scm_name = com.thoughtworks.go.plugin.access.scm.SCMMetadataStore.getInstance().displayValue(scm_plugin_id) %>
                                <li><%= link_to scm_name, '#', :onclick => "Modalbox.show(jQuery('.light_box_content')[0], {overlayClose: false, title: '#{'Add Material'} - #{scm_name}'})" -%></li>
                            <% end %>
                        <% end %>
                    <% end %>
                    <li class="menu_item_last_with_border">
                        <%= link_to 'Pipeline', '#', :onclick => "Modalbox.show('#{admin_dependency_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Pipeline'}'})" -%>
                    </li>
                    <li>
                        <%= link_to 'Package', '#', :onclick => "Modalbox.show('#{admin_package_new_path}', {overlayClose: false, title: '#{'Add Material'} - #{'Package'}'})" -%>
                    </li>

                </ul>
            </div>
        </td>
    </tr>
    </tbody>
</table>
<div class="clear"></div>
</div>
<% if !@pipeline.materialConfigs().scmMaterialsHaveDestination() %>
    <div class="light_box_content" style="display: none">
        <div class="warnings">In order to configure multiple materials for this pipeline, each of its material needs have to a 'Destination Directory' specified. Please edit the existing material and specify a 'Destination Directory' in order to proceed with this operation.</div>
        <button class="right submit close_modalbox_control"><span>Close</span></button>
        <div class="clear"></div>
    </div>
<% end %>

<script type="text/javascript">
    AjaxForm.error_box_selector = '#MB_content';
    Util.on_load(function() {
        var newMaterialPopup = new MicroContentPopup(jQuery('#new_material_popup').get(0), new MicroContentPopup.NoOpHandler());
        var newMaterialPopupShower = new MicroContentPopup.ClickShower(newMaterialPopup);
      <% unless @is_config_repo_pipeline %>
        newMaterialPopupShower.bindShowButton(jQuery('#add_material_dropdown_link').get(0));
      <% end %>
    });
</script>
